package cn.lastcrdg.testitemdecoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2018/4/8 14:45
 * description：
 */
public class ItemDecoration extends RecyclerView.ItemDecoration{

    private Paint mPaint;
    private int mDividerHight = 10;

    public ItemDecoration() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (parent.getChildAdapterPosition(view) != 0) {
            //第一个条目不加分割线
            outRect.top = mDividerHight;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);

            int index = parent.getChildAdapterPosition(view);
            if (index== 0){
                //第一个条目不画线
                continue;
            }

            int dividerLeft = view.getLeft() + view.getPaddingLeft();
            int dividerTop = view.getTop() - mDividerHight;
            int dividerRight = view.getRight() - view.getPaddingRight();
            int dividerBottom = view.getBottom();
            c.drawRect(dividerLeft, dividerTop, dividerRight, dividerBottom, mPaint);
        }
    }
}
